package com.gxa.saas.admin.filter;

import com.gxa.saas.core.common.JsonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class tokenFilter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("This is spring 拦截器！===============================================================================");
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
        System.out.println("token================================"+token);
        JsonResult jsonResult = new JsonResult();
        if (token == null){
            jsonResult.setCode("401");
            jsonResult.setMsg("未登录，请先登录");
            response.sendRedirect(jsonResult.toString());
            return false;
        }
        System.out.println("原始token============================================>"+token);

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {


    }
}
